package com.hele.sts.action;

import java.sql.ResultSet;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.hele.sts.dao.QEkUserDao;
import com.hele.sts.dao.QLogDao;
import com.hele.sts.dao.QUser;
import com.hele.sts.dao.QUserDao;
import com.hele.sts.dao.Qyhznr;
import com.hele.sts.dao.QyhznrDao;
import com.hele.sts.pub.SqlQuery;
import com.hele.sts.pub.StringUtil;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionContext;

public class WebRouterAction implements Action {

	private String nbxh;
	private String zch;
	private String qymc;
	private String djjg;
	private String qylx;
	private String newUrl;

	private static final Logger logger = LoggerFactory.getLogger(WebRouterAction.class);

	@Override
	public String execute() throws Exception {
		ActionContext ctx = ActionContext.getContext();
		Map session = ctx.getSession();

		String userName = (String) session.get("user_name");
		if (StringUtils.isBlank(userName)) {
			return "login";
		}
		if (StringUtils.isBlank(nbxh)) {
			return "error";
		}
		String userId = (String) session.get("userid");
		String yhlx = (String) session.get("yhlx");
		if (zch == null) {
			zch = "";
		}
		QyhznrDao qyhznrDao = new QyhznrDao();
		Qyhznr qyhznr = QyhznrDao.load(nbxh);
		String qymc;
		if(qyhznr==null) {
			qyhznr=QyhznrDao.loadBfTj(nbxh);
		}
		if(qyhznr!=null){
			qymc = qyhznr.getQymc();
		}else{
			qymc = QyhznrDao.getQymc(zch);
		}
		QUser qUser = QUserDao.load(userId);
		ResultSet rs = null;
		//if (yhlx != null && yhlx.equals("1")) {
//			String fullName = null;
//			try {
//				rs = QEkUserDao.loadFullByUserId(userId);
//
//				if (rs.next()) {
//					fullName = rs.getString("username");
//				}
//			} catch (Exception e) {
//				logger.error("查询当前用户出错", e);
//			} finally {
//				if (rs != null)
//					rs.close();
//			}
//			boolean a = QLogDao.insertEx(StringUtil.createLogId(), userId, fullName, yhlx, userName, nbxh, zch, qymc,qUser.getBz());
//			if (!a) {
//				logger.error("写入系统日志失败", a);
//				return "error";
//			}
		//}

		String strategy = "";
		if (qylx.length() <= 0) {
			return "error";
		}
		char fCode = qylx.charAt(0);
		// 策略1 -- 表示远程web处理
		if ((int) fCode >= (int) '5' && (int) fCode <= (int) '7') {
			strategy = "1";
			
//			String lsSql = "select webaddress from xt_server where djjg = '" + djjg + "' and strategy ='1'";
//			// String lsSql =
//			// "select webaddress from xt_server where strategy ='1'";
//
//			SqlQuery sqlQuery = new SqlQuery();
//
//			try {
//				rs = sqlQuery.getResultSet(lsSql);
//				if (rs != null) {
//					if (rs.next()) {
//						String webAddress = rs.getString("webaddress");
//						if (webAddress.lastIndexOf("/") != webAddress.length() - 1) {
//							webAddress += "/";
//						}
//						newUrl = webAddress + "mainFrame.jsp?nbxh=" + nbxh + "&username=" + userName;
//					} else {
//						newUrl = "mainFrame.jsp?nbxh=" + nbxh;
//					}
//				}
//			} catch (Exception e) {
//				e.printStackTrace();
//			} finally {
//				if (rs != null)
//					rs.close();
//				if (sqlQuery != null)
//					sqlQuery.close();
//			}
			newUrl = "mainFrame.jsp?nbxh=" + nbxh;
		} else {
			strategy = "0";
			newUrl = "mainFrame.jsp?nbxh=" + nbxh;
		}

		return "success";
	}

	/**
	 * @return the nbxh
	 */
	public String getNbxh() {
		return nbxh;
	}

	/**
	 * @param nbxh
	 *            the nbxh to set
	 */
	public void setNbxh(String nbxh) {
		this.nbxh = nbxh;
	}

	/**
	 * @return the zch
	 */
	public String getZch() {
		return zch;
	}

	/**
	 * @param zch
	 *            the zch to set
	 */
	public void setZch(String zch) {
		this.zch = zch;
	}

	/**
	 * @return the qymc
	 */
	public String getQymc() {
		return qymc;
	}

	/**
	 * @param qymc
	 *            the qymc to set
	 */
	public void setQymc(String qymc) {
		this.qymc = qymc;
	}

	/**
	 * @return the djjg
	 */
	public String getDjjg() {
		return djjg;
	}

	/**
	 * @param djjg
	 *            the djjg to set
	 */
	public void setDjjg(String djjg) {
		this.djjg = djjg;
	}

	/**
	 * @return the qylx
	 */
	public String getQylx() {
		return qylx;
	}

	/**
	 * @param qylx
	 *            the qylx to set
	 */
	public void setQylx(String qylx) {
		this.qylx = qylx;
	}

	/**
	 * @return the newUrl
	 */
	public String getNewUrl() {
		return newUrl;
	}

	/**
	 * @param newUrl
	 *            the newUrl to set
	 */
	public void setNewUrl(String newUrl) {
		this.newUrl = newUrl;
	}

}
